package chen.lion.hilib.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import chen.lion.hilib.R;

/**
 * 上午11:13
 * by chenxiu
 **/
public class HiPagerTabLayout extends LinearLayout {

    private List<ImageView> mIconViews = new ArrayList<>();

    private List<TextView> mNameViews = new ArrayList<>();

    private List<Integer> mDefultImages = new ArrayList<>() ;

    private List<Integer> mSelectImages = new ArrayList<>()  ;


    private String[] mNames;

    private int mDefultColor,mSelectColor;

    private ViewPager mViewPager;

    public HiPagerTabLayout(Context context) {
        super(context);
    }

    public HiPagerTabLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HiPagerTabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 添加tab按钮
     * @param res 图标
     * @param name 文字
     */
    public void addTab(int res,String name){
        addTab(res,0,name);
    }

    /**
     * 批量添加tab按钮
     * @param ress 图标集合
     * @param names 文字集合
     */
    public void addTabs(int[] ress,String[] names){
        addTabs(ress,null,names);
    }

    /**
     * 添加变色tab按钮
     * @param res 图标
     * @param name 文字
     */
    public void addTab(int res,int selectRes,String name){
        mDefultImages.add(res);
        if(selectRes!=0){
            mSelectImages.add(selectRes);
        }
        View v = LayoutInflater.from(getContext()).inflate(R.layout.hi_tab_view,null);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(0,LayoutParams.MATCH_PARENT);
        layoutParams.weight = 1;
        v.setTag(mNameViews.size());
        TextView text = v.findViewById(R.id.hi_tab_text);
        text.setText(name);
        text.setTextColor(mDefultColor);
        mNameViews.add(text);
        ImageView icon = v.findViewById(R.id.hi_tab_image);
        icon.setImageResource(res);
        if(selectRes>0) {
            icon.setTag(selectRes);
        }
        mIconViews.add(icon);
        v.setLayoutParams(layoutParams);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mViewPager!=null){
                    Integer tag = (Integer) view.getTag();

                    mViewPager.setCurrentItem(tag,false);
                }
            }
        });
        this.addView(v);
        if(mSelectImages.size()>0) {
            mIconViews.get(0).setImageResource(mSelectImages.get(0));
        }
        mNameViews.get(0).setTextColor(mSelectColor);


    }

    /**
     * 批量添加变色tab按钮
     * @param ress 图标集合
     * @param names 文字集合
     */
    public void addTabs(int[] ress,int[] selectRes,String[] names){
        for(int i = 0;i<ress.length;i++){
            if(selectRes!=null) {
                addTab(ress[i], selectRes[i], names[i]);
            }else{
                addTab(ress[i], 0, names[i]);
            }
        }
    }

    /**
     * 设置绑定的ViewPager
     * @param viewPager
     */
    public void setViewPager(ViewPager viewPager){
        mViewPager = viewPager;
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                initNames();
                initImages();
                if(mSelectImages.size()>0) {
                    mIconViews.get(position).setImageResource(mSelectImages.get(position));
                }
                mNameViews.get(position).setTextColor(mSelectColor);
                if(mOnPagerSelected!=null){
                    mOnPagerSelected.onSelect(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public void setOnPagerSelected(OnPagerSelected selected){
        mOnPagerSelected = selected;
    }
    private OnPagerSelected mOnPagerSelected;
    public interface OnPagerSelected{
         void onSelect(int position);
    }


    /**
     * 设置文字颜色
     * @param defultColor 默认颜色
     * @param selectColor
     */
    public void setColors(int defultColor,int selectColor){
        mDefultColor = defultColor;
        mSelectColor = selectColor;
        if(mViewPager == null) {
            return;
        }
        int position = mViewPager.getCurrentItem();
        mNameViews.get(position).setTextColor(selectColor);
    }

    private void initNames(){
        for(TextView textView :mNameViews){
            textView.setTextColor(mDefultColor);
        }
    }

    private void initImages(){
        for(int i = 0; i<mDefultImages.size();i++){
            mIconViews.get(i).setImageResource(mDefultImages.get(i));
        }
    }


    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);

    }

    public void clear(){
        mIconViews.clear();
        mDefultImages.clear();
        mSelectImages.clear();
        mNameViews.clear();
        removeAllViews();
        mViewPager = null;
    }
}
